Allow using --release flag with --example
authorrndr <you@example.com>
Sat, 3 Jan 2015 16:33:08 +0000 (21:33 +0500)
committerrndr <you@example.com>
Tue, 6 Jan 2015 05:29:11 +0000 (10:29 +0500)
Closes #831

src/bin/run.rs
src/cargo/core/manifest.rs
src/cargo/ops/cargo_run.rs
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/util/toml.rs
tests/test_cargo_compile.rs
tests/test_cargo_profiles.rs
tests/test_cargo_run.rs

index 473d847bdea33fd9644f2b472a19d210c0c830f5..7125b68ea9810aa8f66d00b965ca6c66164c5fd9 100644 (file)
@@ -50,12 +50,11 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
     shell.set_verbose(options.flag_verbose);
     let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path));
 
-    let env = if options.flag_example.is_some() {
-        "test"
-    } else if options.flag_release {
-        "release"
-    } else {
-        "compile"
+    let env = match (options.flag_release, options.flag_example.is_some()) {
+        (true, true) => "bench",
+        (true, false) => "release",
+        (false, true) => "test",
+        (false, false) => "compile"
     };
 
     let mut compile_opts = ops::CompileOptions {
index 48b7a7f55e48f93d155b79e4a59b235c9d097e5b..9fabe6bb9075744f0c26d6b5fcd393b7919571d2 100644 (file)
@@ -178,6 +178,13 @@ impl Profile {
         }
     }
 
+    pub fn default_example() -> Profile {
+        Profile {
+            test: false,
+            .. Profile::default_test()
+        }
+    }
+
     pub fn default_bench() -> Profile {
         Profile {
             env: "bench".to_string(),
@@ -188,6 +195,13 @@ impl Profile {
         }
     }
 
+    pub fn default_example_release() -> Profile {
+        Profile {
+            test: false,
+            .. Profile::default_bench()
+        }
+    }
+
     pub fn default_release() -> Profile {
         Profile {
             env: "release".to_string(),
index 706a74fa79f8cc1d4f31210d7022aedf6cfbba53..772828c23c868b48a4393ebe1c314b2871cfbd80 100644 (file)
@@ -39,11 +39,13 @@ pub fn run(manifest_path: &Path,
     let dst = manifest_path.dir_path().join("target");
     let dst = match options.target {
         Some(target) => dst.join(target),
-        None => if bin.is_example() { dst.join("examples") } else { dst },
+        None => dst,
     };
-    let exe = match bin.get_profile().get_dest() {
-        Some(s) => dst.join(s).join(bin.get_name()),
-        None => dst.join(bin.get_name()),
+    let exe = match (bin.get_profile().get_dest(), bin.is_example()) {
+        (Some(s), true) => dst.join(s).join("examples").join(bin.get_name()),
+        (Some(s), false) => dst.join(s).join(bin.get_name()),
+        (None, true) => dst.join("examples").join(bin.get_name()),
+        (None, false) => dst.join(bin.get_name()),
     };
     let exe = match exe.path_relative_from(&try!(os::getcwd())) {
         Some(path) => path,
index c3d43e2e54155b69fb9f925549a8da4ab8c109a2..dc7649e0cccd148fc6873237878ee47f98f42fb3 100644 (file)
@@ -4,6 +4,7 @@ use std::dynamic_lib::DynamicLibrary;
 use std::io::USER_RWX;
 use std::io::fs::{self, PathExtensions};
 use std::sync::Arc;
+use std::path;
 
 use core::{SourceMap, Package, PackageId, PackageSet, Target, Resolve};
 use util::{self, CargoResult, human, caused_human};
@@ -783,7 +784,7 @@ fn build_deps_args(mut cmd: CommandPrototype, target: &Target, package: &Package
             v.push_all(target.get_name().as_bytes());
             v.push(b'=');
             v.push_all(layout.root().as_vec());
-            v.push(b'/');
+            v.push(path::SEP_BYTE);
             v.push_all(filename.as_bytes());
             cmd = cmd.arg("--extern").arg(v.as_slice());
         }
index be863795b6573888668f78c97d49916dcf6888d4..899a875cd07a4884bc3fc004ec95ade6bfc0e136 100644 (file)
@@ -814,11 +814,14 @@ fn normalize(libs: &[TomlLibTarget],
         for ex in examples.iter() {
             let path = ex.path.clone().unwrap_or_else(|| PathValue::String(default(ex)));
 
-            let profile = Profile::default_test().test(false);
-            let profile = merge(profile, &profiles.test);
+            let profile = merge(Profile::default_example(), &profiles.test);
+            let profile_release = merge(Profile::default_example_release(), &profiles.release);
             dst.push(Target::example_target(ex.name.as_slice(),
                                             &path.to_path(),
                                             &profile));
+            dst.push(Target::example_target(ex.name.as_slice(),
+                                            &path.to_path(),
+                                            &profile_release));
         }
     }
 
index 4c333dcc39a03558a6d138b3e3d1cb95def02188..2bbf419c795defd3f6ce34b128562f77979c0bc3 100644 (file)
@@ -873,9 +873,9 @@ test!(verbose_release_build_deps {
         --emit=dep-info,link \
         -L dependency={dir}{sep}target{sep}release \
         -L dependency={dir}{sep}target{sep}release{sep}deps \
-        --extern foo={dir}{sep}target{sep}release{sep}deps/\
+        --extern foo={dir}{sep}target{sep}release{sep}deps{sep}\
                      {prefix}foo-[..]{suffix} \
-        --extern foo={dir}{sep}target{sep}release{sep}deps/libfoo-[..].rlib`
+        --extern foo={dir}{sep}target{sep}release{sep}deps{sep}libfoo-[..].rlib`
 ",
                     running = RUNNING,
                     compiling = COMPILING,
index 5479fafe98ca50ed9ce80a2b0ed2605d0e0335ce..c4ee0edb37de34708956ef1bc7efdb0a64f6cc9d 100644 (file)
@@ -101,9 +101,9 @@ test!(top_level_overrides_deps {
         --emit=dep-info,link \
         -L dependency={dir}{sep}target{sep}release \
         -L dependency={dir}{sep}target{sep}release{sep}deps \
-        --extern foo={dir}{sep}target{sep}release{sep}deps/\
+        --extern foo={dir}{sep}target{sep}release{sep}deps{sep}\
                      {prefix}foo-[..]{suffix} \
-        --extern foo={dir}{sep}target{sep}release{sep}deps/libfoo-[..].rlib`
+        --extern foo={dir}{sep}target{sep}release{sep}deps{sep}libfoo-[..].rlib`
 ",
                     running = RUNNING,
                     compiling = COMPILING,
index 26ab98e205c73f87a8f82da9d5acdeb4a07b6ed0..bc727a9e68e49e06d6933f011ee34eafd01574b5 100644 (file)
@@ -220,6 +220,110 @@ hello main.rs
         sep = path::SEP).as_slice()));
 });
 
+test!(example_with_release_flag {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies.bar]
+            version = "*"
+            path = "bar"
+        "#)
+        .file("examples/a.rs", r#"
+            extern crate bar;
+
+            fn main() {
+                if cfg!(ndebug) {
+                    println!("fast1")
+                } else {
+                    println!("slow1")
+                }
+                bar::baz();
+            }
+        "#)
+        .file("bar/Cargo.toml", r#"
+            [project]
+            name = "bar"
+            version = "0.0.1"
+            authors = []
+
+            [lib]
+            name = "bar"
+        "#)
+        .file("bar/src/bar.rs", r#"
+            pub fn baz() {
+                if cfg!(ndebug) {
+                    println!("fast2")
+                } else {
+                    println!("slow2")
+                }
+            }
+        "#);
+
+    assert_that(p.cargo_process("run").arg("-v").arg("--release").arg("--example").arg("a"),
+                execs().with_status(0).with_stdout(format!("\
+{compiling} bar v0.0.1 ({url})
+{running} `rustc src{sep}bar.rs --crate-name bar --crate-type lib \
+        -C opt-level=3 \
+        --cfg ndebug \
+        -C metadata=[..] \
+        -C extra-filename=[..] \
+        --out-dir {dir}{sep}target{sep}release{sep}deps \
+        --emit=dep-info,link \
+        -L dependency={dir}{sep}target{sep}release{sep}deps \
+        -L dependency={dir}{sep}target{sep}release{sep}deps`
+{compiling} foo v0.0.1 ({url})
+{running} `rustc {dir}{sep}examples{sep}a.rs --crate-name a --crate-type bin \
+        -C opt-level=3 \
+        --cfg ndebug \
+        --out-dir {dir}{sep}target{sep}release{sep}examples \
+        --emit=dep-info,link \
+        -L dependency={dir}{sep}target{sep}release \
+        -L dependency={dir}{sep}target{sep}release{sep}deps \
+         --extern bar={dir}{sep}target{sep}release{sep}deps{sep}libbar-[..].rlib`
+{running} `target{sep}release{sep}examples{sep}a`
+fast1
+fast2
+",
+        compiling = COMPILING,
+        running = RUNNING,
+        dir = p.root().display(),
+        url = path2url(p.root()),
+        sep = path::SEP).as_slice()));
+
+    assert_that(p.process(cargo_dir().join("cargo")).arg("run").arg("-v").arg("--example").arg("a"),
+                execs().with_status(0).with_stdout(format!("\
+{compiling} bar v0.0.1 ({url})
+{running} `rustc src{sep}bar.rs --crate-name bar --crate-type lib \
+        -g \
+        -C metadata=[..] \
+        -C extra-filename=[..] \
+        --out-dir {dir}{sep}target{sep}deps \
+        --emit=dep-info,link \
+        -L dependency={dir}{sep}target{sep}deps \
+        -L dependency={dir}{sep}target{sep}deps`
+{compiling} foo v0.0.1 ({url})
+{running} `rustc {dir}{sep}examples{sep}a.rs --crate-name a --crate-type bin \
+        -g \
+        --out-dir {dir}{sep}target{sep}examples \
+        --emit=dep-info,link \
+        -L dependency={dir}{sep}target \
+        -L dependency={dir}{sep}target{sep}deps \
+         --extern bar={dir}{sep}target{sep}deps{sep}libbar-[..].rlib`
+{running} `target{sep}examples{sep}a`
+slow1
+slow2
+",
+        compiling = COMPILING,
+        running = RUNNING,
+        dir = p.root().display(),
+        url = path2url(p.root()),
+        sep = path::SEP).as_slice()));
+});
+
 test!(run_dylib_dep {
     let p = project("foo")
         .file("Cargo.toml", r#"